package com.account.boot.admin.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.account.boot.commons.exception.APIException;
import com.account.boot.commons.exception.APIExceptionUtils;
import com.account.boot.commons.result.APIResultDO;
import com.account.boot.commons.result.ResultCodeEnum;
import com.account.boot.commons.result.ResultDO;
import com.account.boot.interfaces.api.UserService;
import com.account.boot.model.vo.UserVo;

@RestController
@RequestMapping("/user/")
public class UserController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private UserService userService;
	
	/**
	 * 分页获取用户信息
	 * @param index 第几页
	 * @return
	 */
	@RequestMapping("findByPage")
	public Object findByPage(Integer index) {
		APIResultDO apiResultDO = APIResultDO.buildSuccessResult();
		try {
			ResultDO<List<UserVo>> resultDO = userService.findByPage(index);
			if(APIResultDO.CODE_SUCCESS != resultDO.getResultCode()) {
				throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
			}
			apiResultDO.setData(resultDO.getModule());
		} catch (Exception e) {
			APIExceptionUtils.processException(apiResultDO, e	, logger);
		}
		return apiResultDO;
	}
	
	/**
	 * 获取用户详情
	 * @param uid 用户uid
	 * @return
	 */
	@RequestMapping("findByUid")
	public Object findByUid(String uid) {
		APIResultDO apiResultDO = APIResultDO.buildSuccessResult();
		try {
			if(StringUtils.isEmpty(uid)) {
				throw new APIException(ResultCodeEnum.UID_EMPTY);
			}
			ResultDO<UserVo> resultDO = userService.findByUid(uid);
			if(APIResultDO.CODE_SUCCESS != resultDO.getResultCode()) {
				throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
			}
			apiResultDO.setData(resultDO.getModule());
		} catch (Exception e) {
			APIExceptionUtils.processException(apiResultDO, e, logger);
		}
		return apiResultDO;
	}
	
	/**
	 * 保存用户信息
	 * @param userVo
	 * @return
	 */
	@RequestMapping("save")
	public Object save(UserVo userVo) {
		APIResultDO apiResultDO = APIResultDO.buildSuccessResult();
		return apiResultDO;
	}
}
